//
//  LGDiscoverController.m
//  MindBox
//
//  Created by MS on 15-12-31.
//  Copyright (c) 2015年 Lemon. All rights reserved.
//
#define DurTime 0.3
#import "LGDiscoverController.h"
#import "GoodsDetailController.h"
#import "GoodsController.h"
#import "WelfareDetailController.h"
#import "WelfareForcaseController.h"
#import "MindDetailController.h"
#import "DiscoverPageTool.h"
#import "DiscoverSessionView.h"
#import "DiscoverRaiderView.h"
#import "RaidersDetailController.h"
@interface LGDiscoverController ()<DiscoverSessionViewDelegate,DiscoverRaiderViewDelegate>
@property (nonatomic,weak) UIView * mainView;
@property (nonatomic,weak) DiscoverSessionView * sessionView;
@property (nonatomic,weak) DiscoverRaiderView * raidersView;
@property (nonatomic,strong) NSMutableArray * activityArray;
@property (nonatomic,strong) NSMutableArray * raidersArray;
@property (nonatomic,assign) int pages;
@property (nonatomic,assign) int raidersPage;
@property (nonatomic,weak)  MJRefreshAutoFooter * footerFresh;
@property (nonatomic,weak)  MJRefreshNormalHeader * headerRefresh;
@property (nonatomic,strong) Discover_Welfare * welfareData;
@end

@implementation LGDiscoverController

-(NSMutableArray *)activityArray
{
    if (_activityArray==nil) {
        _activityArray = [[NSMutableArray alloc]init];
    }
    return _activityArray;
}

-(NSMutableArray *)raidersArray
{
    if (_raidersArray==nil) {
        _raidersArray = [[NSMutableArray alloc]  init];
    }
    return _raidersArray;
}

-(DiscoverSessionView *)sessionView
{
    if (_sessionView==nil) {
        DiscoverSessionView * view = [[DiscoverSessionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT-49)];
        [self.mainView addSubview:view];
        _sessionView = view;
        _sessionView.delegate = self;
    }
    return _sessionView;
}

-(DiscoverRaiderView *)raidersView
{
    if (_raidersView==nil) {
        DiscoverRaiderView * view = [[DiscoverRaiderView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HIGHT-49)];
        [self.mainView addSubview:view];
        _raidersView = view;
        _raidersView.delegate = self;
    }
    return _raidersView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor purpleColor];
    UIView * mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2*WIDTH, HIGHT-49)];
    _mainView = mainView;
    [self.view addSubview:mainView];
    
    [self setUpNavBar];
    
//    [self loadDiscoverWelfareData];
    
    [self loadDiscoverMindData];
    
    [self loadDiscoverRaidersData];
    
    [self addFootRefresh];
    [self addHeaderRefresh];
}

-(void)loadDiscoverWelfareData
{
    [DiscoverPageTool discoverWelfareDataWithSuccess:^(Discover_Welfare *data) {
        [_footerFresh endRefreshing];
        [_headerRefresh endRefreshing];
        
        self.welfareData = data;
        self.sessionView.welfare = data;

    } failure:^(NSError *error) {
        
    }];
}

-(void)loadDiscoverMindData
{
    [DiscoverPageTool discoverMindDataWithPages:++_pages Success:^(Discover_Mind *data){
        [_footerFresh endRefreshing];
        [_headerRefresh endRefreshing];
        
        if (data.activity.count) {
            [self.activityArray addObjectsFromArray:data.activity];
        }
        self.sessionView.activityArray = _activityArray;

    } failure:^(NSError *error) {
        
        
    }];
}

-(void)loadDiscoverRaidersData
{
    [DiscoverPageTool discoverRaidersDataWithPages:++_raidersPage Success:^(NSArray *data) {
        
        [_raidersView.tableView.mj_header endRefreshing];
        [_raidersView.tableView.mj_footer endRefreshing];
        
        if (data.count) {
            
            [self.raidersArray addObjectsFromArray:data];
            
        }
        self.raidersView.raiderArrary = self.raidersArray;
        [self.raidersView.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

-(void)addFootRefresh
{
    __weak typeof(self) weakSelf = self;
    MJRefreshAutoNormalFooter * footerFresh = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadDiscoverMindData];
        
    }];
    
    self.footerFresh = footerFresh;
    self.sessionView.tableView.mj_footer = footerFresh;
   
    self.raidersView.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadDiscoverRaidersData];
    }];
;
}

-(void)addHeaderRefresh
{
    __weak typeof(self) weakSelf = self;
    
    MJRefreshNormalHeader * headerRefresh = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pages = 0;
        [weakSelf.activityArray removeAllObjects];
//        [weakSelf loadDiscoverWelfareData];
        [weakSelf loadDiscoverMindData];
    }];
    self.headerRefresh = headerRefresh;
    self.sessionView.tableView.mj_header = headerRefresh;
    
    self.raidersView.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.raidersPage = 0;
        [weakSelf.raidersArray removeAllObjects];
        [weakSelf loadDiscoverRaidersData];
    }];
;

}

#pragma mark SessionView代理方法

-(void)discoverWelfareSessionViewDidClick:(DiscoverSessionView *)discoverSessionView welfare:(Discover_Welfare *)welfare discover_goods:(Discover_Goods *)welfareGoods index:(NSInteger)index
{
    if (index<self.welfareData.live.count) {
        WelfareDetailController * vc = [[WelfareDetailController alloc] init];
        vc.title = welfareGoods.title;
        vc.wel_cat_id = welfareGoods.table_id;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else{
        WelfareForcaseController * vc = [[WelfareForcaseController alloc] init];
        vc.forecaseArray = self.welfareData.forecast;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];

    }

}

-(void)discoverMindSessionViewDidClick:(DiscoverSessionView *)discoverSessionView activity:(Activity *)activity goods:(Goods *)goods index:(NSInteger)index
{
    if (index==-1) {
        MindDetailController * vc = [[MindDetailController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.title = activity.title;
        vc.activity_id = activity.theme_id;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    else{
        
        GoodsController * vc  = [[GoodsController alloc] init];
        vc.good_id = goods.goods_id;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark RaiderView代理方法

-(void)discoverRaiderViewDidClick:(DiscoverRaiderView *)discoverRaiderView raider:(Discover_Raiders *)raider index:(NSInteger)index
{
    
    RaidersDetailController * vc = [[RaidersDetailController alloc] init];
    vc.urlStr = raider.html_src;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)setUpNavBar
{
    UISegmentedControl * segement = [[UISegmentedControl alloc] initWithItems:@[@"专场",@"攻略"]];
    segement.tintColor = [UIColor colorWithRed:0.98f green:0.33f blue:0.34f alpha:1.00f];
    segement.frame = CGRectMake(0, 0, self.view.width*0.55, 25);
    segement.selectedSegmentIndex = 0;
    [segement addTarget:self action:@selector(segeMentClick:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segement;
}

-(void)segeMentClick:(UISegmentedControl *)segement
{
    if (segement.selectedSegmentIndex==1) {
        [UIView animateWithDuration:DurTime animations:^{
            self.mainView.transform = CGAffineTransformMakeTranslation(-WIDTH, 0);
        }];
    }
    
    else{
        [UIView animateWithDuration:DurTime animations:^{
            self.mainView.transform = CGAffineTransformIdentity;
        }];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
